package com.huiquan.management.controller;

import java.io.IOException;
import java.util.HashMap;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import com.huiquan.framework.base.ReturnData;
import com.huiquan.framework.utils.RequestParamUtil;
import com.huiquan.management.service.MigrateService;

@Controller
public class MigrateController {

	@Autowired
	private MigrateService migrateService;

	@SuppressWarnings("unchecked")
	@RequestMapping(value = "/management/migrate_list.htm")
	public ModelAndView list(HttpServletRequest req, HttpServletResponse resp) throws IOException {
		String tabid = req.getParameter("tabid");
		// 维护页面参数到session
		Map<String, Object> pageParam;
		if (tabid != null && !tabid.isEmpty()) {
			pageParam = new HashMap<>();
			pageParam.put("tabid", tabid);

			req.getSession().setAttribute("pageParam", pageParam);
		} else {
			pageParam = (Map<String, Object>) req.getSession().getAttribute("pageParam");
		}

		String keyword = RequestParamUtil.getParamByRequestOrSession(req, pageParam, "keyword");

		return migrateService.list(keyword);
	}

	@ResponseBody
	@RequestMapping(value = "/management/migrate_update.do")
	public ReturnData update(HttpServletRequest req, HttpServletResponse resp) throws Exception {
		String id = req.getParameter("id");
		String username = req.getParameter("username");
		String type = req.getParameter("type");
		String location = req.getParameter("location");

		return migrateService.update(Long.parseLong(id), username, type, location);
	}

	@ResponseBody
	@RequestMapping(value = "/management/migrate_excute.do")
	public ReturnData excute(HttpServletRequest req, HttpServletResponse resp) throws Exception {

		String code = req.getParameter("code");

		return migrateService.excute(code);
	}

}
